package com.example.sbvueblog.controller;

import com.example.sbvueblog.dao.BlogRepository;
import com.example.sbvueblog.dto.BlogDTO;
import com.example.sbvueblog.entity.Blog;
import com.example.sbvueblog.response.R;
import com.example.sbvueblog.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin("http://localhost:5173")
@RequestMapping("blog")
public class BlogController {

    @Autowired
    BlogRepository blogRepository;

    @Autowired
    BlogService blogService;

    @GetMapping("/list")
    public R list(){
        List<Blog> blogs = blogRepository.findAll();
        R r = new R();
        r.setPosts(blogs);
        return r;
    }

    @PostMapping("add")
    public void add(@RequestBody BlogDTO blogDTO){
        blogService.add(blogDTO);
    }

    @DeleteMapping("delete/{id}")
    public void delete(@PathVariable Long id){
        blogRepository.deleteById(id);
    }
}
